﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Segmentation.UI.Forms
{
    public partial class PleaseWaitForm : Form
    {
        #region Fields

        private Timer timer;

        #endregion Fields

        #region Constructors

        public PleaseWaitForm(string message)
        {
            InitializeComponent();

            this._lPleaseWait.Text = message;

            if (Application.RenderWithVisualStyles)
            {
                this._progressBar.Style = ProgressBarStyle.Marquee;
            }
            else
            {
                this.timer = new Timer();
                this.timer.Interval = 500;
                this.timer.Tick += new EventHandler(timer_Tick);
                this.timer.Start();
                this.FormClosing += new FormClosingEventHandler(PleaseWaitForm_FormClosing);
            }
        }

        #endregion Constructors

        #region Event Handlers

        private void timer_Tick(object sender, EventArgs e)
        {
            if (_progressBar.Value != _progressBar.Maximum)
            {
                _progressBar.Value++;
            }
            else
            {
                _progressBar.Value = _progressBar.Minimum;
            }
        }

        private void PleaseWaitForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.timer.Enabled = false;
        }

        private void PleaseWaitForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 27)
            {
                this.Close();
            }
        }

        #endregion Event Handlers

        #region Methods

        public void CloseOnComplete(IAsyncResult result)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(this.Close));
            }
            else
            {
                this.Close();
            }
        }

        public AsyncCallback GetCallbackMethod()
        {
            return new AsyncCallback(this.CloseOnComplete);
        }

        #endregion Methods
    }
}
